import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/list/section.dart';
import 'package:industry_net/pages/news/main/models/comment.dart';
import 'package:industry_net/pages/news/news_detail/widgets/cell.dart';
import 'package:industry_net/pages/news/news_detail/widgets/comment_bar/controller.dart';

import 'index.dart';

class NewsReplyController extends GetxController {
  NewsReplyController();
  final controller = CommentController();
  final state = NewsReplyState();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  void observer() {
    state.scrollCtrl.addListener(() {
      var _offy = state.scrollCtrl.offset;
      if (_offy > 45.r) {
        if (!state.isShowUser) {
          state.isShowUser = true;
          update();
        }
      } else {
        if (state.isShowUser) {
          state.isShowUser = false;
          update();
        }
      }
    });
  }

  void getData() {
    var _items = List.generate(10, (index) => NewsCommentModel());
    state.dataSource = [
      SectionModel(
          index: 0,
          itemBuilder: (context, index) {
            var _md = _items[index];
            return NewsCommentItemView(
              key: GlobalKey(),
              md: _md,
              padding: EdgeInsets.symmetric(horizontal: gap12, vertical: gap10),
              callback: actionCellClick,
              onPraise: actionPraise,
            );
          },
          items: _items)
    ];
    update();
  }

  void actionCellClick(GlobalKey key, NewsCommentModel md) async {
    RenderBox _box = key.currentContext!.findRenderObject() as RenderBox;
    var _offy = _box.localToGlobal(Offset(0, _box.size.height)).dy;

    controller.updateHinText('回复上岸必有我：');
    controller.state.focusNode.requestFocus();
    await Future.delayed(const Duration(milliseconds: 200));

    var _keyboardHeight = MediaQuery.of(Get.context!).viewInsets.bottom;
    var _visibleOff = Get.height - buttonHeight - _keyboardHeight;
    if (_offy > _visibleOff) {
      var _shouldOff = state.scrollCtrl.offset + _offy - _visibleOff;
      state.scrollCtrl.jumpTo(_shouldOff);
    }
  }

  void actionPraise(NewsCommentModel md) async {
    md.isPraised = !md.isPraised;
    md.isAnimating = true;
    update();
    await Future.delayed(const Duration(milliseconds: 300));
    md.isAnimating = false;
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    observer();
    super.onInit();
    // state.md = Get.arguments;
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    getData();
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    state.scrollCtrl.dispose();
    state.streamCtrl.close();
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
